const app = getApp(),url = app.globalData.url;
Page({
  data: {
    id:0,//熟练度id
    image:'',//显示
    title:'',
    time:'',
    certificateUrl:'',//上传
    time:'',//参加工作时间
  },

  onLoad: function (options) {
    console.log(options)
    var that = this;
    that.setData({ id:options.id });
   
    if(options.id > 0){
      wx.request({
        url: url + 'cvCertification/getCVCertificateById',
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("证书详情",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              title:ret.data.title,	
              time:ret.data.time,
              certificateUrl:ret.data.certificateUrl,
              image:ret.data.certificateUrl,
            })
          }else{
            // that.setData({xldetail:''})
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      console.log('=0')
      return false
    }
  },
  //技能名称
  title(e){
    var that = this
    that.setData({title:e.detail.value})
  },
  // 获得证书时间
  // time(e){
  //   var that = this
  //   that.setData({time:e.detail.value})
  // },
  bindtimeChange: function(e) {
    console.log('bindtimeChange', e.detail.value)
    this.setData({
      time: e.detail.value,
      // time: e.detail.value
    })
  },
  // /dgdx-Api/cvCertification/uploadCertificate//上传证书
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          console.log(res)
        if(res.tempFilePaths.length > 0){
            // console.log(2)
            that.setData({image:res.tempFilePaths[0]})
            that.uploadFile(res.tempFilePaths[0])
        }
      }
    })
  },
  uploadFile: function(tempUrl){
    var that = this
    console.log(tempUrl)
    wx.uploadFile({
      url:url+'cvCertification/uploadCertificate',
      filePath: tempUrl,
      name: 'cv_certificate',
      success: function (res) {
        var ret=JSON.parse(res.data)
        console.log(ret)
        if (ret.code == 200) {
            console.log(ret.data)
            that.setData({certificateUrl:ret.data})
            wx.showToast({
              title: 'ok',
              icon:'none',
            })
        }
      }
    });
  },
    
  // 删除
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvCertification/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title:'删除失败',
            icon:'none',
          })
        }
      }
    })
  },
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(e){
    var that = this
    var xlid = that.data.id
    if(xlid > 0){
      //编辑
      var method = 'PUT'
      var api = 'cvCertification/updateCertificate'
      var xlid = xlid
    }else{
      // 添加
      var method = 'POST'
      var api = 'cvCertification/add'
      var xlid = 0
    }
    var uid = wx.getStorageSync('user_massage')
    if(that.data.title == '' || that.data.time ==''){//that.data.certificateUrl == '' || 
      wx.showToast({
        title: '请上传全部信息！！',
        icon:'none',
      })
      return false
    }
    let sensWords = Object.values(this.sensWords || {})
      if(sensWords.length > 0){
        wx.showToast({
          title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
          icon:'none',
        })
        return false;
      }
    var data = {
      approval: '1',
      certificateUrl:'',//图片
      title:that.data.title,	//title
      time:that.data.time,	//time
      userUid:uid.userUid,	//用户编号
      id:xlid,//id
      createTime:'',
      updateTime:''
    }
    wx.request({
      url:url + api,
      header:{
      'context-type': 'application/json',
      },
      method: method,
      data:data,
      success:function(res){
        var ret = res.data
        console.log(res,'证书 ')
        if(ret.code == 200){
          wx.showToast({
            title:'保存成功',
            icon:'none',
          })
          setTimeout(function(){
            // wx.redirectTo({
            //   url:'/HomePage/linejianli/linejianli'
            // })
            wx.navigateBack({
              delta: 1,
            })
          },1000)
        }else{
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })
  },
  
  
    

    
})